#############################################
#This file creates Figure 3
#############################################

###Log
my_log <- file("figure3_log.txt")

sink(my_log, append=T, type="output")
sink(my_log, append=T, type="message")

cat(readChar(rstudioapi::getSourceEditorContext()$path, # Writing currently opened R script to file
             file.info(rstudioapi::getSourceEditorContext()$path)$size))

start_time <- Sys.time()
start_time


library(foreign)
library(magrittr)
library(multcomp)
library(ggstance)
library(socviz)
library(rio)
library(broom)
library(margins)
library(tidyverse)

############################
#Experiment 3
############################

####Load the data
exp3 <- import("./Data/exp3_cleaned.csv")

####Clean the data as needed
#restrict to partisans
exp3 <- exp3 %>%
  filter(pid_3 == "Democrat" | pid_3 == "Republican")

#DV
exp3$support01 <- scales::rescale(exp3$tax_support)

#Treatment Condition(s)
exp3$treat_5 <- factor(exp3$treat_5, 
                     levels=c("No Party Cue", "Party Cue", 
                              "Interest Groups", "Companies", 
                              "Labor Unions"))

exp3$treat_policy <- factor(exp3$change, 
                            levels=c("Conservative Change", "Liberal Change"))

exp3$stereo <- factor(exp3$change_stereotype, 
                      levels=c("Non-Stereotypical Change", "Stereotypical Change"), 
                      labels=c("Counter-Stereotypical", "Stereotypical"))

#to Stata for tables
exp3_s <- exp3 %>% select(treat_5, support01, stereo, pid_2)
export(exp3_s, "./Figure3/figure3_data.dta")


#############
#Models and lincoms
#############

m1 <- tidy(lm(support01 ~ treat_5 + stereo, data = exp3)) %>%
  filter(term !="(Intercept)" & term != "stereoStereotypical") %>%
  mutate(comparison = "Coefficient", 
         partisan = "All Partisans", 
         term = socviz::prefix_strip(term, "treat_5"))

##Differences
m1_a <- lm(support01 ~ treat_5 + stereo, data = exp3)
names(coef(m1_a))

m1_b <- tidy(summary(glht(m1_a, linfct=c("`treat_5Party Cue` - `treat_5Interest Groups` = 0")))) %>%
  mutate(comparison = "Difference", 
         partisan = "All Partisans") %>%
  rename(p.value = adj.p.value) %>%
  mutate(term = dplyr::recode(contrast, "`treat_5Party Cue` - `treat_5Interest Groups`" = "Interest Groups")) %>%
  dplyr::select(-contrast, -null.value)

m1_c <- tidy(summary(glht(m1_a, linfct=c("`treat_5Party Cue` - treat_5Companies = 0")))) %>%
  mutate(comparison = "Difference", 
         partisan = "All Partisans") %>%
  rename(p.value = adj.p.value) %>%
  mutate(term = dplyr::recode(contrast, "`treat_5Party Cue` - treat_5Companies" = "Companies"))  %>%
  dplyr::select(-contrast, -null.value)


m1_d <- tidy(summary(glht(m1_a, linfct=c("`treat_5Party Cue` - `treat_5Labor Unions` = 0")))) %>%
  mutate(comparison = "Difference", 
         partisan = "All Partisans") %>%
  rename(p.value = adj.p.value) %>%
  mutate(term = dplyr::recode(contrast, "`treat_5Party Cue` - `treat_5Labor Unions`" = "Labor Unions"))  %>%
  dplyr::select(-contrast, -null.value)


###########Democrats
###Model
d1 <- tidy(lm(support01 ~ treat_5 + stereo, data = exp3, subset=(pid_2 == "Democrat"))) %>%
  filter(term !="(Intercept)" & term != "stereoStereotypical") %>%
  mutate(comparison = "Coefficient", 
         partisan = "Democrats", 
         term = prefix_strip(term, "treat_5"))

##Differences
d1_a <- lm(support01 ~ treat_5 + stereo, data = exp3, subset=(pid_2 == "Democrat"))
names(coef(d1_a))

d1_b <- tidy(summary(glht(d1_a, linfct=c("`treat_5Party Cue` - `treat_5Interest Groups` = 0")))) %>%
  mutate(comparison = "Difference", 
         partisan = "Democrats") %>%
  rename(p.value = adj.p.value) %>%
  mutate(term = dplyr::recode(contrast, "`treat_5Party Cue` - `treat_5Interest Groups`" = "Interest Groups")) %>%
  dplyr::select(-contrast, -null.value)


d1_c <- tidy(summary(glht(d1_a, linfct=c("`treat_5Party Cue` - treat_5Companies = 0")))) %>%
  mutate(comparison = "Difference", 
         partisan = "Democrats") %>%
  rename(p.value = adj.p.value) %>%
  mutate(term = dplyr::recode(contrast, "`treat_5Party Cue` - treat_5Companies" = "Companies"))  %>%
  dplyr::select(-contrast, -null.value)


d1_d <- tidy(summary(glht(d1_a, linfct=c("`treat_5Party Cue` - `treat_5Labor Unions` = 0")))) %>%
  mutate(comparison = "Difference", 
         partisan = "Democrats") %>%
  rename(p.value = adj.p.value) %>%
  mutate(term = dplyr::recode(contrast, "`treat_5Party Cue` - `treat_5Labor Unions`" = "Labor Unions"))  %>%
  dplyr::select(-contrast, -null.value)


######Republicans

###Model
r1 <- tidy(lm(support01 ~ treat_5 + stereo, data = exp3, subset=(pid_2 == "Republican"))) %>%
  filter(term !="(Intercept)" & term != "stereoStereotypical") %>%
  mutate(comparison = "Coefficient", 
         partisan = "Republicans", 
         term = prefix_strip(term, "treat_5"))

##Differences
r1_a <- lm(support01 ~ treat_5 + stereo, data = exp3, subset=(pid_2 == "Republican"))
names(coef(d1_a))

r1_b <- tidy(summary(glht(r1_a, linfct=c("`treat_5Party Cue` - `treat_5Interest Groups` = 0")))) %>%
  mutate(comparison = "Difference",  
         partisan = "Republicans") %>%
  rename(p.value = adj.p.value) %>%
  mutate(term = dplyr::recode(contrast, "`treat_5Party Cue` - `treat_5Interest Groups`" = "Interest Groups")) %>%
  dplyr::select(-contrast, -null.value)


r1_c <- tidy(summary(glht(r1_a, linfct=c("`treat_5Party Cue` - treat_5Companies = 0")))) %>%
  mutate(comparison = "Difference", 
         partisan = "Republicans") %>%
  rename(p.value = adj.p.value) %>%
  mutate(term = dplyr::recode(contrast, "`treat_5Party Cue` - treat_5Companies" = "Companies"))  %>%
  dplyr::select(-contrast, -null.value)


r1_d <- tidy(summary(glht(r1_a, linfct=c("`treat_5Party Cue` - `treat_5Labor Unions` = 0")))) %>%
  mutate(comparison = "Difference", 
         partisan = "Republicans") %>%
  rename(p.value = adj.p.value) %>%
  mutate(term = dplyr::recode(contrast, "`treat_5Party Cue` - `treat_5Labor Unions`" = "Labor Unions"))  %>%
  dplyr::select(-contrast, -null.value)

#####Combine
results <- rbind(m1, m1_b, m1_c, m1_d, 
                 d1, d1_b, d1_c, d1_d, 
                 r1, r1_b, r1_c, r1_d) %>%
  mutate(lower = estimate - (1.96*std.error), 
         upper = estimate + (1.96*std.error), 
         sig = if_else(p.value < 0.05, "Significant p < 0.05", 
                       "Insignificant p >= 0.05"))


#####Figure
results %>%
  mutate(term = factor(term, 
                       levels=c("Labor Unions", "Companies", "Interest Groups", 
                                "Party Cue"))) %>%
  ggplot(aes(x=estimate, y=term, shape=sig)) + 
  geom_pointrangeh(aes(xmin=lower, xmax=upper)) + 
  theme_bw(18) + 
  facet_grid(comparison ~ partisan, 
             labeller = label_wrap_gen(width = 15), 
             space="free_y", scales="free_y") + 
  geom_text(data=results, aes(label=round(estimate,2)), 
            nudge_y=0.15)  + 
  theme(strip.text.y = element_text(angle=0), 
        legend.position="bottom") + 
  scale_shape_manual(values=c(1,16)) + 
  labs(x = NULL,
       y = NULL, 
       shape = "Significant") + 
  geom_vline(xintercept=0, 
             linetype="dashed", 
             color="red") + 
  scale_x_continuous(breaks=c(-0.1, 0, 0.1))

ggsave("./Figure3/figure3.png", height=7, width=14)

end_time <- Sys.time()
end_time

closeAllConnections()

